package com.cortex.pineapple.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cortex.pineapple.R;
import com.cortex.pineapple.view.FiltersView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class CompositeSourceAdapter extends BaseAdapter implements OnClickListener{

	private String[] sources;
	private Context context;
	private static HashMap<String, Boolean> sourcesMap;
	private OnSelectedSourcesChangedListener onSelectedSourcesChangedListener;

	public CompositeSourceAdapter(Context context, String[] sources) {
		this.context = context;
		this.sources = sources;
		if(sourcesMap == null){
			sourcesMap = new HashMap<String, Boolean>();
			clearSelectedSources();
		}

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return sources.length;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.view_item_source, null);

			holder = new ViewHolder();
			holder.sourceText = (TextView) convertView.findViewById(R.id.sourceText);
			holder.selectSource = (CheckBox) convertView.findViewById(R.id.selectSource);
			convertView.setTag(holder);

			convertView.setOnClickListener(this);
			holder.selectSource.setOnClickListener(this);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		String source = sources[position];
		holder.sourceText.setText(source);
		holder.selectSource.setChecked(sourcesMap.get(source));

		return convertView;

	}
	@Override
	public void onClick(View v) {
		ViewGroup parent;
		if((v instanceof ViewGroup)){
			parent = (ViewGroup)v;
		}
		else{
			parent = (ViewGroup) v.getParent();
		}
		String key = ((TextView) parent.findViewById(R.id.sourceText)).getText().toString();
		boolean newValue = !sourcesMap.get(key);
		boolean isAllSelected = false;
		// change first check box to new value 
		if(!newValue){
			sourcesMap.put(sources[0],newValue);
			ViewGroup child = (ViewGroup) ((ViewGroup)parent.getParent()).getChildAt(0);
			CheckBox selectSource = (CheckBox) child.findViewById(R.id.selectSource);
			selectSource.setChecked(newValue);
		}
		// change all check box in list to new value 
		if(key.equals(sources[0])){
			for (int i = 0; i < sources.length; i++) {
				sourcesMap.put(sources[i], newValue);
			}
			// update check box
			for (int i = 0; i < ((ViewGroup) parent.getParent()).getChildCount(); i++) {
				ViewGroup child = (ViewGroup) ((ViewGroup)parent.getParent()).getChildAt(i);
				CheckBox selectSource = (CheckBox) child.findViewById(R.id.selectSource);
				selectSource.setChecked(newValue);
			}
			isAllSelected = true;
		}
		// change specific check box in list to new value
		else{
			// update source
			sourcesMap.put(key, newValue);
			CheckBox selectSource = (CheckBox) parent.findViewById(R.id.selectSource);
			selectSource.setChecked(newValue);
		}
		// update view on changed sources
		if(onSelectedSourcesChangedListener != null){
			List<String> sourcesSelected = new ArrayList<String>();
			for (String source : sourcesMap.keySet()) {
				if(sourcesMap.get(source) && !source.equals(sources[0])){
					sourcesSelected.add(source);
				}
			}
			onSelectedSourcesChangedListener.sourcesChanged(sourcesSelected,isAllSelected);
		}
	}
	public void setOnSelectedSourcesChangedListener(OnSelectedSourcesChangedListener onSelectedSourcesChangedListener) {
		this.onSelectedSourcesChangedListener = onSelectedSourcesChangedListener;
	}
	public void clearSelectedSources(){
		for (String source : sources) {
			sourcesMap.put(source, false);
		}
	}
	private class ViewHolder {
		TextView sourceText;
		CheckBox selectSource;
	}

	public interface OnSelectedSourcesChangedListener{
		void sourcesChanged(List<String> sources,boolean isAll);
	}


}
